<script setup lang="ts">
import RoomStatus from './components/RoomStatus.vue'
import RoomAction from './components/RoomAction.vue'
import RoomMessage from './components/RoomMessage.vue'

import { ref, onUnmounted, onMounted } from 'vue'
import { io } from 'socket.io-client'
import { useUserStore } from '@/stores'

let socket: any
const store = useUserStore()
const list = ref<any[]>([])
onUnmounted(() => {
  socket.close()
})
onMounted(async () => {
  // 建立链接，创建 socket.io 实例
  socket = io('https://consult-api.itheima.net', {
    auth: {
      token: `Bearer ${store.user?.token}`
    },
    query: {
      orderId: '6928651375497216'
    }
  })

  socket.on('connect', () => {
    // 建立连接成功
    console.log('connect')
  })

  socket.on('chatMsgList', (res) => {
    console.log(
      '这里是我们一旦连接上了ws 服务器，服务器就会自动给我 发送emit一个事件，名称是chatMsgList。 我们患者在这里监听这个事件，就能得到之前消息的历史记录'
    )
    console.log(res)
  })

  socket.on('error', (event) => {
    // 错误异常消息
    console.log('error')
  })

  socket.on('disconnect', () => {
    // 已经断开连接
    console.log('disconnect')
  })
})

// 6928651375497216
// /room?orderId=6928651375497216&payResult=true&type=2
</script>

<template>
  <div class="room-page">
    <cp-nav-bar title="问诊室" />
    <room-status></room-status>
    <room-message></room-message>
    <room-action></room-action>
  </div>
</template>

<style lang="scss" scoped>
.room-page {
  // padding-top: 90px;
  padding-bottom: 60px;
  min-height: 100vh;
  box-sizing: border-box;
  background-color: var(--cp-bg);
  .van-pull-refresh {
    width: 100%;
    min-height: calc(100vh - 150px);
  }
}
</style>
